<!--  -->

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Stock Broker implementation for the Stock Quoter Publisher/Subscriber Real-time CORBA Service</title>
</head>

<body
 text = "#000000"
 link = "#000fff"
 vLink= "#ff0f0f"
 aLink = "#0000ff"
 bgColor = "#ffffff">

<hr><h2>Stock Broker implementation for the Stock Quoter Publisher/Subscriber Real-time CORBA Service</h2><hr>

<h3>Implementation of StockBrokerHome interface</h3>

This interface is used to register the necessary factories and mappings with the specified orb and
create StockBroker objects. <P>

The Stock_StockBrokerHome_i class is generated automatically by the IDL compiler (using the -GI flag),
which is a subclass of POA_Stock::StockBrokerHome class. <P>

This class is also a subclass of ACE_Event_Handler, which can be used as an event handler. <P>

<h4>Implementing the Constructor</h4>

The main steps of this function are described as follows: <P>

<li>Register the necessary factories and mappings with the specified orb.</li>

<PRE>
    Stock::StockName_init *stockname_factory = new Stock::StockName_init;
    orb->register_value_factory (stockname_factory->tao_repository_id (),
                                 stockname_factory);

    Stock::Cookie_init *cookie_factory = new Stock::Cookie_init;
    orb->register_value_factory (cookie_factory->tao_repository_id (),
                                 cookie_factory;

    Stock_PriorityMapping::register_mapping (orb);
</PRE>

<li>Register this class as an event handler with the ORB to catch ctrl-c from the command line. </li>

<PRE>
    if (orb_->orb_core ()->reactor ()->register_handler (SIGINT, this) == -1)
      ACE_DEBUG ((LM_DEBUG, "ERROR: Failed to register as a signal handler: %p\n",
                  "register_handler\n"));
</PRE>

<h4>Implementing the create () member function</h4>

<li>If there is no instance of the StockBroker object in the StockBrokerHome object then create one.
The "orb_", "dist", and "stock_name" are in the arguments of the create () function.
Since the broker has nothing to do with any of the RTCORBA mechanisms, we can activate it under the
default_POA, which is the RootPOA. </li>

<PRE>
    Stock_StockBroker_i *broker = new Stock_StockBroker_i (orb_, dist, stock_name);
    PortableServer::ServantBase_var owner_transfer = broker;
    this->broker_ = broker->_this ();
</PRE>

<li>Return the StockBroker object. </li>

<PRE>
    return Stock::StockBroker::_duplicate (this->broker_.in ());
</PRE>

<h4>Implementing the handle_signal () member function</h4>

<li>Shutdown the broker object and the orb used by it. </li>

<PRE>
    this->broker_->shutdown ();

    this->orb_->shutdown (false);
</PRE>

<hr><h3>Implementation of StockBroker interface</h3>

This interface is used for Stock Broker client. <P>
The Stock_StockBroker_i class is generated automatically by the IDL compiler (using the -GI flag),
which is a subclass of POA_Stock::StockBroker class. <P>

<h4>Implementing the Constructor</h4>

<PRE>
    Stock_StockBroker_i (CORBA::ORB_ptr orb,
                         Stock::StockDistributor_ptr dist,
                         const char *stock_name)
      : orb_ (CORBA::ORB::_duplicate (orb)),
        quoter_ (Stock::StockQuoter::_nil()),
        consumer_ (0),
        distributor_ (Stock::StockDistributor::_duplicate (dist))
</PRE>

The "quoter_", "consumer_", and "distributor_" are four private members of the Stock_StockBroker_i class.
They stand for the StockQuoter object that is used to get detailed stock information, the StockNameConsumer
object that is used to get notification of updates, and the StockDistributor object that the StockBroker object
is used to unsubscribe to. <P>

The main steps of this function are described as follows: <P>

<li>Get a reference to the RTORB.</li>

<li>Initialize a CORBA::PolicyList object.</li>

<li>Create a CLIENT_PROPAGATED priority model policy and add it into the former CORBA::PolicyList object.</li>

<li>Create a child POA using the former CORBA::PolicyList and narrow it to RTPOA.</li>

<li>Create a new instance of the StockNameConsumer object with the specified name and activate it
under the former RTPOA.</li>

<PRE>
    CORBA::Object_var obj = orb->resolve_initial_references ("RTORB");
    RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow (obj.in ());

    TAO::Utils::PolicyList_Destroyer consumer_policies (1);
    consumer_policies.length (1);

    consumer_policies[0]
      = rt_orb->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
                                              Stock::Priority_Mapping::MEDIUM);

    PortableServer::POA_var poa = this->_default_POA ();
    PortableServer::POAManager_var poa_mgr = poa->the_POAManager ();

    PortableServer::POA_var child_poa
      = poa->create_POA ("StockNameConsumer_POA",
                         poa_mgr.in (),
                         consumer_policies);

    RTPortableServer::POA_var rt_poa = RTPortableServer::POA::_narrow (child_poa.in ());

    this->consumer_ = new Stock_StockNameConsumer_i (*this, stock_name);
    PortableServer::ServantBase_var nameconsumer_owner_transfer = this->consumer_;
    rt_poa->activate_object (this->consumer_);
</PRE>

<h4>Implementing the get_consumer_notifier () member function</h4>

<li>Return the StockNameConsumer object created by the Constructor.</li>

<PRE>
    Stock::StockNameConsumer_var consumer = this->consumer_->_this ();
    return consumer._retn();
</PRE>

<h4>Implementing the connect_quoter_info () member function</h4>

<li>Duplicate a StockQuoter object using the StockQuoter object reference "c" in the argument.</li>

<PRE>
    quoter_ = Stock::StockQuoter::_duplicate (c);
</PRE>

<h4>Implementing the disconnect_quoter_info () member function</h4>

<li>Destroy the StockQuoter object and return it.</li>

<PRE>
    Stock::StockQuoter_var old_quoter = this->quoter_;
    this->quoter_ = Stock::StockQuoter::_nil();
    return old_quoter._retn ();
</PRE>

<h4>Implementing the get_connection_quoter_info () member function</h4>

<li>Return the StockQuoter object.</li>

<PRE>
    return Stock::StockQuoter::_duplicate (this->quoter_);
</PRE>

<h4>Implementing the shutdown () member function</h4>

<li>Unsubscribe the StockBroker object.</li>

<PRE>
    this->distributor_->unsubscribe_notifier (this->consumer_->cookie ());
</PRE>

<li>Deactivate the StockBroker object.</li>

<PRE>
    ::Stock::StockBroker_var broker = this->_this ();
    PortableServer::ObjectId_var oid = this->_default_POA ()->reference_to_id (broker.in ());

    this->_default_POA ()->deactivate_object (oid.in ());
</PRE>

<hr><b>Email: </b><a href="mailto:"</a<ADDRESS>shanshan.jiang@vanderbilt.edu</ADDRESS>

</body>

</html>
